/*     */ package com.newcrm.excel;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.xml.parsers.ParserConfigurationException;
/*     */ import javax.xml.parsers.SAXParser;
/*     */ import javax.xml.parsers.SAXParserFactory;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
/*     */ import org.apache.poi.openxml4j.opc.OPCPackage;
/*     */ import org.apache.poi.openxml4j.opc.PackageAccess;
/*     */ import org.apache.poi.ss.usermodel.BuiltinFormats;
/*     */ import org.apache.poi.ss.usermodel.DataFormatter;
/*     */ import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
/*     */ import org.apache.poi.xssf.eventusermodel.XSSFReader;
/*     */ import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
/*     */ import org.apache.poi.xssf.model.StylesTable;
/*     */ import org.apache.poi.xssf.usermodel.XSSFCellStyle;
/*     */ import org.apache.poi.xssf.usermodel.XSSFRichTextString;
/*     */ import org.xml.sax.Attributes;
/*     */ import org.xml.sax.ContentHandler;
/*     */ import org.xml.sax.InputSource;
/*     */ import org.xml.sax.SAXException;
/*     */ import org.xml.sax.XMLReader;
/*     */ import org.xml.sax.helpers.DefaultHandler;
/*     */ 
/*     */ public class XLSXReader extends AbstractExcelReader
/*     */ {
/*  34 */   private static Logger logger = Logger.getLogger(XLSXReader.class.getName());
/*     */   private OPCPackage xlsxPackage;
/* 289 */   private List<String[]> rows = null;
/*     */ 
/*     */   public XLSXReader(File file) {
/* 292 */     this.rows = new ArrayList();
/*     */     try {
/* 294 */       this.xlsxPackage = OPCPackage.open(file, PackageAccess.READ);
/*     */     } catch (InvalidFormatException e) {
/* 296 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream)
/*     */     throws IOException, ParserConfigurationException, SAXException
/*     */   {
/* 312 */     InputSource sheetSource = new InputSource(sheetInputStream);
/* 313 */     SAXParserFactory saxFactory = SAXParserFactory.newInstance();
/* 314 */     SAXParser saxParser = saxFactory.newSAXParser();
/* 315 */     XMLReader sheetParser = saxParser.getXMLReader();
/* 316 */     ContentHandler handler = new MyXSSFSheetHandler(styles, strings, this);
/* 317 */     sheetParser.setContentHandler(handler);
/* 318 */     sheetParser.parse(sheetSource);
/*     */   }
/*     */ 
/*     */   public List<String[]> parse()
/*     */     throws Exception
/*     */   {
/* 330 */     ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(
/* 331 */       this.xlsxPackage);
/* 332 */     XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
/* 333 */     StylesTable styles = xssfReader.getStylesTable();
/* 334 */     XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator)xssfReader
/* 335 */       .getSheetsData();
/*     */ 
/* 337 */     if (iter.hasNext()) {
/* 338 */       InputStream stream = iter.next();
/* 339 */       processSheet(styles, strings, stream);
/* 340 */       stream.close();
/*     */     }
/*     */ 
/* 343 */     return this.rows;
/*     */   }
/*     */ 
/*     */   private static class MyXSSFSheetHandler extends DefaultHandler
/*     */   {
/*     */     private StylesTable stylesTable;
/*     */     private ReadOnlySharedStringsTable sharedStringsTable;
/*     */     private boolean vIsOpen;
/*     */     private XLSXReader.xssfDataType nextDataType;
/*     */     private short formatIndex;
/*     */     private String formatString;
/*     */     private final DataFormatter formatter;
/*  76 */     private int thisColumn = -1;
/*     */ 
/*  78 */     private int lastColumnNumber = -1;
/*     */     private StringBuffer value;
/*  83 */     private XLSXReader reader = null;
/*  84 */     private List<String> row = null;
/*     */ 
/*     */     public MyXSSFSheetHandler(StylesTable styles, ReadOnlySharedStringsTable strings, XLSXReader reader)
/*     */     {
/* 100 */       this.stylesTable = styles;
/* 101 */       this.sharedStringsTable = strings;
/* 102 */       this.value = new StringBuffer();
/* 103 */       this.nextDataType = XLSXReader.xssfDataType.NUMBER;
/* 104 */       this.formatter = new DataFormatter();
/*     */ 
/* 106 */       this.reader = reader;
/* 107 */       this.row = new ArrayList();
/*     */     }
/*     */ 
/*     */     public void startElement(String uri, String localName, String name, Attributes attributes)
/*     */       throws SAXException
/*     */     {
/* 120 */       if (("inlineStr".equals(name)) || ("v".equals(name))) {
/* 121 */         this.vIsOpen = true;
/*     */ 
/* 123 */         this.value.setLength(0);
/* 124 */       } else if (name.equals("row")) {
/* 125 */         this.row.clear();
/*     */       }
/* 127 */       else if ("c".equals(name))
/*     */       {
/* 129 */         String r = attributes.getValue("r");
/* 130 */         int firstDigit = -1;
/* 131 */         for (int c = 0; c < r.length(); c++) {
/* 132 */           if (Character.isDigit(r.charAt(c))) {
/* 133 */             firstDigit = c;
/* 134 */             break;
/*     */           }
/*     */         }
/* 137 */         this.thisColumn = nameToColumn(r.substring(0, firstDigit));
/*     */ 
/* 139 */         this.nextDataType = XLSXReader.xssfDataType.NUMBER;
/* 140 */         this.formatIndex = -1;
/* 141 */         this.formatString = null;
/* 142 */         String cellType = attributes.getValue("t");
/* 143 */         String cellStyleStr = attributes.getValue("s");
/* 144 */         if ("b".equals(cellType)) {
/* 145 */           this.nextDataType = XLSXReader.xssfDataType.BOOL;
/* 146 */         } else if ("e".equals(cellType)) {
/* 147 */           this.nextDataType = XLSXReader.xssfDataType.ERROR;
/* 148 */         } else if ("inlineStr".equals(cellType)) {
/* 149 */           this.nextDataType = XLSXReader.xssfDataType.INLINESTR;
/* 150 */         } else if ("s".equals(cellType)) {
/* 151 */           this.nextDataType = XLSXReader.xssfDataType.SSTINDEX;
/* 152 */         } else if ("str".equals(cellType)) {
/* 153 */           this.nextDataType = XLSXReader.xssfDataType.FORMULA;
/* 154 */         } else if (cellStyleStr != null)
/*     */         {
/* 157 */           int styleIndex = Integer.parseInt(cellStyleStr);
/* 158 */           XSSFCellStyle style = this.stylesTable.getStyleAt(styleIndex);
/* 159 */           this.formatIndex = style.getDataFormat();
/* 160 */           this.formatString = style.getDataFormatString();
/* 161 */           if (this.formatString == null)
/* 162 */             this.formatString = 
/* 163 */               BuiltinFormats.getBuiltinFormat(this.formatIndex);
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/*     */     public void endElement(String uri, String localName, String name)
/*     */       throws SAXException
/*     */     {
/* 178 */       String thisStr = null;
/*     */ 
/* 181 */       if ("v".equals(name))
/*     */       {
/* 184 */         switch (this.nextDataType.ordinal()) {
/*     */         case 1:
/* 186 */           char first = this.value.charAt(0);
/* 187 */           thisStr = first == '0' ? "FALSE" : "TRUE";
/* 188 */           break;
/*     */         case 2:
/* 191 */           thisStr = "";
/* 192 */           break;
/*     */         case 3:
/* 194 */           thisStr = this.value.toString();
/* 195 */           break;
/*     */         case 4:
/* 197 */           XSSFRichTextString rtsi = new XSSFRichTextString(
/* 198 */             this.value.toString());
/* 199 */           thisStr = rtsi.toString();
/* 200 */           break;
/*     */         case 5:
/* 202 */           String sstIndex = this.value.toString();
/*     */           try {
/* 204 */             int idx = Integer.parseInt(sstIndex);
/* 205 */             XSSFRichTextString rtss = new XSSFRichTextString(
/* 206 */               this.sharedStringsTable.getEntryAt(idx));
/* 207 */             thisStr = rtss.toString();
/*     */           } catch (NumberFormatException ex) {
/* 209 */             XLSXReader.logger.warn("Failed to parse SST index", ex);
/*     */           }
/*     */ 
/*     */         case 6:
/* 213 */           String n = this.value.toString();
/* 214 */           if (this.formatString != null) {
/* 215 */             if (this.formatString.equals("m/d/yy")) {
/* 216 */               this.formatString = "yyyy-mm-dd";
/*     */             }
/* 218 */             thisStr = this.formatter.formatRawCellContents(
/* 219 */               Double.parseDouble(n), this.formatIndex, 
/* 220 */               this.formatString);
/*     */           } else {
/* 222 */             thisStr = n;
/*     */           }
/* 224 */           break;
/*     */         default:
/* 226 */           thisStr = "";
/*     */         }
/*     */ 
/* 232 */         if (this.lastColumnNumber == -1) {
/* 233 */           this.lastColumnNumber = 0;
/*     */         }
/*     */ 
/* 237 */         for (int i = this.lastColumnNumber + 1; i < this.thisColumn; i++) {
/* 238 */           this.row.add("");
/*     */         }
/*     */ 
/* 242 */         if (thisStr != null)
/* 243 */           this.row.add(thisStr.trim());
/*     */         else {
/* 245 */           this.row.add("");
/*     */         }
/*     */ 
/* 248 */         if (this.thisColumn > -1)
/* 249 */           this.lastColumnNumber = this.thisColumn;
/*     */       }
/* 251 */       else if ("row".equals(name)) {
/* 252 */         this.reader.rows.add((String[])this.row.toArray(new String[0]));
/* 253 */         this.lastColumnNumber = -1;
/*     */       }
/*     */     }
/*     */ 
/*     */     public void characters(char[] ch, int start, int length)
/*     */       throws SAXException
/*     */     {
/* 264 */       if (this.vIsOpen)
/* 265 */         this.value.append(ch, start, length);
/*     */     }
/*     */ 
/*     */     private int nameToColumn(String name)
/*     */     {
/* 275 */       int column = -1;
/* 276 */       for (int i = 0; i < name.length(); i++) {
/* 277 */         int c = name.charAt(i);
/* 278 */         column = (column + 1) * 26 + c - 65;
/*     */       }
/* 280 */       return column;
/*     */     }
/*     */   }
/*     */ 
/*     */   static enum xssfDataType
/*     */   {
/*  41 */     BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.excel.XLSXReader
 * JD-Core Version:    0.6.1
 */